<template>
  <div>
    <div class="queryBox">
      <div class="row">
        <div class="left">
          <el-icon>
            <Search />
          </el-icon>
          <span> 筛选搜索 </span>
        </div>
        <div class="right">
          <el-button @click="reset">重置</el-button>
          <el-button type="primary" @click="inquire">查询搜索</el-button>
        </div>
      </div>
      <div class="row">
        <span>活动名称：</span>
        <el-input v-model="flashParams.keyword" placeholder="活动名称" />
      </div>
    </div>
  </div>
  <div class="editUser">
    <div class="editUserLeft">
      <el-icon>
        <Tickets />
      </el-icon>
      <span>数据列表</span>
    </div>
    <div>
      <el-button @click="router.push('/sms/flashSession')"
        >秒杀时间段列表</el-button
      >
      <el-button @click="add">添加活动</el-button>
    </div>
  </div>
  <el-table :data="list" style="width: 100%" border select-on-indeterminate>
    <el-table-column type="selection" />
    <el-table-column prop="id" label="编号" />
    <el-table-column prop="title" label="活动标题" />
    <el-table-column prop="" label="活动状态">活动未开始</el-table-column>
    <el-table-column prop="createTime" label="开始时间">
      <template #default="{ row }">
        <span>{{ formateDate(row.createTime) }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="endDate" label="结束时间">
      <template #default="{ row }">
        <span>{{ formateDate(row.endDate) }}</span>
      </template>
    </el-table-column>
    <el-table-column #default="{ row }" label="是否启用">
      <el-switch
        v-model="row.status"
        :active-value="1"
        :inactive-value="0"
        @change=""
      />
    </el-table-column>
    <el-table-column label="操作" width="260%">
      <template #default="{ row }">
        <el-button text type="primary" @click="toflashPromotionId(row.id)"
          >设置商品</el-button
        >
        <el-button text type="primary" @click="editList(row)">编辑</el-button>
        <el-button text type="primary" @click="deleteFn(row.id)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <div class="page">
    <ElConfigProvider :locale="zhCn">
      <el-pagination
        v-model:current-page="flashParams.pageNum"
        v-model:page-size="flashParams.pageSize"
        :page-sizes="[5, 10, 15]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="pageChangeHandle"
        @current-change="pageSizeChangeHandle"
      />
    </ElConfigProvider>
  </div>
  <FlashEdit
    :show="showFlashEdit"
    @close="closeFn"
    :id="editId"
    @updateList="pageChangeHandle"
  />
</template>
<script setup lang="ts">
import { ElMessage, ElConfigProvider, ElMessageBox } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { reactive, ref } from "vue";
import { flashListApi, deleteFlashListApi } from "./api";
import useTable from "@/hooks/useTable";
import { formateDate } from "@/utils";
import FlashEdit from "./components/FlashEdit.vue";
import { useRouter } from "vue-router";
const router = useRouter();
// 总数有多少条
const flashParams = reactive<IFlashParams>({
  keyword: null,
  pageNum: 1,
  pageSize: 5,
});

const createFlash = () => {};
createFlash();

// 显示隐藏编辑添加弹出框
const showFlashEdit = ref(false);
const editId = ref<number>(0);

// 隐藏编辑添加弹出框
const closeFn = () => {
  showFlashEdit.value = false;
  // 清除id区分编辑和添加
  editId.value = 0;
};

// 添加
const add = () => {
  showFlashEdit.value = true;
};

// 数据列表
const flashList = ref<flashList[]>([]);

// 编辑
const editList = (val: IEditFlashList) => {
  editId.value = val.id!;
  showFlashEdit.value = true;
};

// 删除
const deleteFn = (id: number) => {
  ElMessageBox.confirm("是否确认删除", "提示", {
    cancelButtonText: "取消",
    confirmButtonText: "确认",
    type: "warning",
  })
    .then(() => {
      deleteFlashListApi(id).then((res) => {
        if (res.code === 200) {
          ElMessage.success("删除成功");
          // useTable里面重新请求刷新列表
          pageChangeHandle();
        }
      });
    })
    .catch(() => {});
};

// 查询搜索
const inquire = () => {
  searchHandle();
};

// 重置
const reset = () => {
  flashParams.keyword = "";
  // useTable里的查询
  searchHandle();
};

// 设置商品按钮
const toflashPromotionId = (id: number) => {
  router.push(`/sms/selectSession?flashPromotionId=${id}`);
};

const { list, total, pageChangeHandle, pageSizeChangeHandle, searchHandle } =
  useTable(flashListApi, flashParams);
</script>
<style lang="less" scoped>
.queryBox {
  box-shadow: 0 0 4px 2px #ccc;
  border-radius: 5px;
  padding: 20px;
  margin-bottom: 20px;

  .row {
    display: flex;
    padding: 10px 0;

    &:first-child {
      justify-content: space-between;
      align-items: center;
    }

    &:last-child {
      width: 400px;
      align-items: center;

      span {
        font-size: 12px;
        width: 100px;
      }
    }
  }
}

.editUser {
  display: flex;
  justify-content: space-between;
  box-shadow: 0 0 4px 2px #ccc;
  border-radius: 5px;
  padding: 20px;
  margin-bottom: 20px;
  align-items: center;
}
:deep(.cell) {
  text-align: center;
}

.page {
  padding: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
